package com.maya.screens;

import java.util.Locale;


import com.maya.R;
import com.maya.utilities.Utilities;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class PreferencesScreen extends PreferenceActivity implements OnSharedPreferenceChangeListener 
{
	Utilities util = new Utilities();

	protected PF settingsFragment = new PF();
    
    private MediaPlayer sound;
    private boolean soundIsOn = true;
     
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {			
        super.onCreate(savedInstanceState);
        
        try {
            getClass().getMethod("getFragmentManager");
            AddResourceApi11AndGreater();
        } catch (NoSuchMethodException e) {
            AddResourceApiLessThan11();
        }
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
		
		if(getSoundPreference(getApplicationContext())!=0)
			soundIsOn = true;
		else
			soundIsOn = false;	
    }
	
	@SuppressWarnings("deprecation")
    protected void AddResourceApiLessThan11()
    {
		 addPreferencesFromResource(R.xml.preferences);
    }

    @TargetApi(11)
    protected void AddResourceApi11AndGreater()
    {
        getFragmentManager().beginTransaction().replace(android.R.id.content, settingsFragment).commit();
    }

    @TargetApi(11)
    public static class PF extends PreferenceFragment
    {       
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
	
	@Override     
	protected void onResume() 
	{
	    super.onResume();
	    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	protected void onPause() 
	{
        super.onPause(); 
    }
	
	@Override
    public void onDestroy() 
	{
        PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
        super.onDestroy();
	}
	
	@Override
	public void onBackPressed() 
	{
		finish();
		startActivity(new Intent(PreferencesScreen.this, MainScreen.class));
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
	{	
		if(soundIsOn)
        	util.playSound(sound, getApplicationContext(), R.raw.pref);
		
		if (key.equals(Constants.KEY_LIST_PREFERENCE_DIFFICULTY))
		{       
            SharedPreferences.Editor prefEditor = sharedPreferences.edit(); 
            prefEditor.putString(Constants.KEY_LIST_PREFERENCE_DIFFICULTY, PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.KEY_LIST_PREFERENCE_DIFFICULTY, "2")); 
            prefEditor.commit(); 
		}
		else if (key.equals(Constants.KEY_LIST_PREFERENCE_LANG)) 
        {   
            SharedPreferences.Editor prefEditor = sharedPreferences.edit(); 
            prefEditor.putString(Constants.KEY_LIST_PREFERENCE_LANG, PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.KEY_LIST_PREFERENCE_LANG, "TR")); 
            prefEditor.commit();
                      
            if(PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.KEY_LIST_PREFERENCE_LANG, "TR").equals("TR"))
            {
            	Locale locale = new Locale("tr"); 
	            Locale.setDefault(locale);
	            Configuration config = new Configuration();
	            config.locale = locale;
	            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            }
            else if(PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.KEY_LIST_PREFERENCE_LANG, "TR").equals("EN"))
            {
            	Locale locale = new Locale("en"); 
	            Locale.setDefault(locale);
	            Configuration config = new Configuration();
	            config.locale = locale;
	            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            
            }
            else if(PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.KEY_LIST_PREFERENCE_LANG, "TR").equals("FR"))
            {
            	Locale locale = new Locale("fr"); 
	            Locale.setDefault(locale);
	            Configuration config = new Configuration();
	            config.locale = locale;
	            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());         
            }
            else
            {
            	Locale locale = new Locale("tr"); 
	            Locale.setDefault(locale);
	            Configuration config = new Configuration();
	            config.locale = locale;
	            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
	        	Toast.makeText(this, "Varsayilan dil T�rk�e ayarland�", Toast.LENGTH_LONG).show();
            }
                                 
            finish();    
            Intent intent = new Intent(this, PreferencesScreen.class);    
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
            startActivity(intent);
           	
        }
		
        else if (key.equals(Constants.KEY_LIST_PREFERENCE_SOUND)) 
        {  
            SharedPreferences.Editor prefEditor = sharedPreferences.edit(); 
            prefEditor.putString(Constants.KEY_LIST_PREFERENCE_SOUND, PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.KEY_LIST_PREFERENCE_SOUND, "0")); 
            prefEditor.commit();
        }
        else
        	Toast.makeText(PreferencesScreen.this,"Bir �eyler de�i�ti???",Toast.LENGTH_LONG).show();
		
	}
		
	/* Zorluk Seviyesi */
	public int getZorlukSeviyesi(Context con)
	{
	    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(con);
	    String s = pref.getString(Constants.KEY_LIST_PREFERENCE_DIFFICULTY, "2"); 
        return Integer.parseInt(s); 
	}
	
	public int getBasamakSayisi(Context con)
	{
		switch(getZorlukSeviyesi(con))
		{
			case 1:
				return Constants.BASAMAK_SAYISI_KOLAY_DUZEY;
			
			case 2:
				return Constants.BASAMAK_SAYISI_NORMAL_DUZEY;
							
			case 3:
				return Constants.BASAMAK_SAYISI_ZOR_DUZEY;
							
			default:
				return Constants.BASAMAK_SAYISI_NORMAL_DUZEY;
		}
	}
	
	public int getMaxTahminSuresi(Context con)
	{
		switch(getZorlukSeviyesi(con))
		{
			case 1:
				return Constants.MAX_TAHMIN_SURESI_KOLAY_DUZEY;
				
			case 2:
				return Constants.MAX_TAHMIN_SURESI_NORMAL_DUZEY;
							
			case 3:
				return Constants.MAX_TAHMIN_SURESI_ZOR_DUZEY;
							
			default:
				return Constants.MAX_TAHMIN_SURESI_NORMAL_DUZEY;
		}		
	}
	
	/* Dil Se�imi */
	public String getLanguagePreference(Context con)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(con);
	    String s = pref.getString(Constants.KEY_LIST_PREFERENCE_LANG, "TR"); 
        return s; 
	}
	
	/* Ses Se�imi */
	public int getSoundPreference(Context con)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(con);
	    String s = pref.getString(Constants.KEY_LIST_PREFERENCE_SOUND, "0"); 
        return Integer.parseInt(s);
	}
	
	public void setLanguageFromPreferences(Context con)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(con);
	    String s = pref.getString(Constants.KEY_LIST_PREFERENCE_LANG, "TR");
	    Locale locale = null;
	    
	    if(s.equals("TR"))
	    {
	    	locale = new Locale("tr"); 
	    }
	    else if(s.equals("EN"))
	    {
	    	locale = new Locale("en"); 
	    }
	    else if(s.equals("FR"))
	    {
	    	locale = new Locale("fr"); 
	    }
	    else
	    {
	    	locale = new Locale("tr");
	    }
	    		    
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        con.getResources().updateConfiguration(config, con.getResources().getDisplayMetrics());     		
	}
}

/*
 * SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(PreferenceDemoActivity.this);
   String username = prefs.getString("username", "Default NickName");
   String passw = prefs.getString("password", "Default Password");
   boolean checkBox = prefs.getBoolean("checkBox", false);
   String listPrefs = prefs.getString("listpref", "Default list prefs");

   StringBuilder builder = new StringBuilder();
   builder.append("Username: " + username + "\n");
   builder.append("Password: " + passw + "\n");
   builder.append("Keep me logged in: " + String.valueOf(checkBox) + "\n");
   builder.append("List preference: " + listPrefs);

   textView.setText(builder.toString());
   
   TextView settingsTextView = (TextView) findViewById(R.id.settings_text_view);
   settingsTextView.setText(builder.toString());
 * */
